"use strict";

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function random(min, max) {
	return Math.floor(min + Math.random() * (max - min));
}

var Plan = function () {
	function Plan(hp, x, y, width, height, boomimage, imagesrc) {
		_classCallCheck(this, Plan);

		this.x = x;
		this.y = y;
		this.node = null;
		this.hp = hp;
		this.width = width;
		this.height = height;
		this.boomimage = boomimage;

		this.node = document.createElement("img");
		this.node.style.left = this.x + "px";
		this.node.style.top = this.y + "px";
		this.node.src = imagesrc;
	}

	_createClass(Plan, [{
		key: "moveTo",
		value: function moveTo(x, y) {
			this.x = x;
			this.y = y;
			this.node.style.left = x + "px";
			this.node.style.top = y + "px";
		}
	}]);

	return Plan;
}();

var Player = function (_Plan) {
	_inherits(Player, _Plan);

	function Player(x, y, color) {
		_classCallCheck(this, Player);

		var _this = _possibleConstructorReturn(this, (Player.__proto__ || Object.getPrototypeOf(Player)).call(this, 1, 120, 495, 66, 80, "image/bz.gif", "image/my.gif"));

		_this.color = color;
		return _this;
	}

	_createClass(Player, [{
		key: "shoot",
		value: function shoot() {
			var newbullet = new Bullet(this.x + this.width / 2, this.y);
			game.bullets.push(newbullet);
			game.addToGame(newbullet);
		}
	}]);

	return Player;
}(Plan);

var Bullet = function () {
	function Bullet(x, y) {
		_classCallCheck(this, Bullet);

		this.x = x;
		this.y = y;
		this.node = null;
		this.width = 6;
		this.height = 14;
		this.out = false;
		this.move = function () {
			this.node.style.top = this.node.offsetTop - 20 + "px";
			if (this.node.offsetTop < 0) {
				this.out = true;
			}
			this.x = this.node.offsetTop;
			this.y = this.node.offsetLeft;
		};
		this.node = document.createElement("img");
		this.node.style.left = this.x + "px";
		this.node.style.top = this.y + "px";
		this.node.src = "image/bullet1.png";
	}

	_createClass(Bullet, [{
		key: "move",
		value: function move() {
			this.node.style.top = this.node.offsetTop - 20 + "px";
			if (this.node.offsetTop < 0) {
				this.out = true;
			}
			this.x = this.node.offsetTop;
			this.y = this.node.offsetLeft;
		}
	}]);

	return Bullet;
}();

var Enemy = function (_Plan2) {
	_inherits(Enemy, _Plan2);

	function Enemy(width, height, boomimage, imagesrc, speed, hp) {
		_classCallCheck(this, Enemy);

		var _this2 = _possibleConstructorReturn(this, (Enemy.__proto__ || Object.getPrototypeOf(Enemy)).call(this, 1, random(0, 320 - width / 2), -100, width, height, boomimage, imagesrc));

		_this2.speed = speed;
		_this2.hp = hp;
		_this2.isdead = false;
		_this2.animateTime = 360;
		return _this2;
	}

	_createClass(Enemy, [{
		key: "rush",
		value: function rush() {
			this.node.style.top = this.node.offsetTop + this.speed + "px";
			if (this.node.offsetTop > 568) {
				this.out = true;
			}
			this.x = this.node.offsetTop;
			this.y = this.node.offsetLeft;

			if (this.hp <= 0) {
				if (this.animateTime <= 0) {
					this.isdead = true;
				} else {
					this.animateTime -= 20;
				}
			}
		}
	}, {
		key: "hert",
		value: function hert(_hert) {
			this.hp -= _hert;
			if (this.hp == 0) {
				this.node.src = this.boomimage;
			}
		}
	}]);

	return Enemy;
}(Plan);

var game = {
	score: 0,
	mainDiv: document.getElementById("maindiv"),
	player: new Player(),
	bullets: [],
	enemys: [],
	frame: 0,
	init: function init() {
		this.mainDiv.appendChild(this.player.node);
		this.mainDiv.addEventListener("mousemove", function () {
			game.bindMouse();
		});
	},
	addToGame: function addToGame(e) {
		this.mainDiv.appendChild(e.node);
	},
	bindMouse: function bindMouse() {
		var x = event.clientX - this.player.width / 2;
		var y = event.clientY - this.player.height / 2;
		this.player.moveTo(x, y);
	},
	refresh: function refresh() {
		this.frame++;
		//init bullet 
		if (this.frame % 5 == 0) {
			this.player.shoot();
		}

		if (this.frame % 20 == 0) {
			var newEnemy = new Enemy(34, 24, "image/xx.gif", "image/enemy1_fly_1.png", random(1, 4), 1);
			this.enemys.push(newEnemy);
			this.addToGame(newEnemy);
		}

		if (this.frame % 50 == 0) {
			var newEnemy = new Enemy(46, 60, "image/zz.gif", "image/enemy3_fly_1.png", random(1, 3), 6);
			this.enemys.push(newEnemy);
			this.addToGame(newEnemy);
		}

		if (this.frame % 100 == 0) {
			var newEnemy = new Enemy(110, 164, "image/dd.gif", "image/enemy2_fly_1.png", 1, 12);
			this.enemys.push(newEnemy);
			this.addToGame(newEnemy);
		}

		for (var b in this.bullets) {
			var currentBullet = this.bullets[b];
			currentBullet.move();
			if (currentBullet.out) {
				this.mainDiv.removeChild(currentBullet.node);
				this.bullets.splice(b, 1);
			}

			for (var e in this.enemys) {
				var currentEnemy = this.enemys[e];
				if (currentBullet.node.offsetLeft > currentEnemy.node.offsetLeft && currentBullet.node.offsetLeft < currentEnemy.node.offsetLeft + currentEnemy.width) {
					if (currentBullet.node.offsetTop > currentEnemy.node.offsetTop && currentBullet.node.offsetTop < currentEnemy.node.offsetTop + currentEnemy.height) {
						currentEnemy.hert(1);
					}
				}
			}
		}
		for (var e in this.enemys) {
			this.enemys[e].rush();

			if (this.enemys[e].out || this.enemys[e].isdead) {
				this.mainDiv.removeChild(this.enemys[e].node);
				this.enemys.splice(e, 1);
			}
		}
	}
};
game.init();
setInterval(function () {
	game.refresh();
}, 20);
